from rest_framework.generics import ListAPIView
from rest_framework.response import Response
from .models import SKU
from .serializers import SKUListSerializer
from meiduo_mall.utils.pagination import StandardResultsSetPagination
from rest_framework.filters import OrderingFilter
from drf_haystack.viewsets import HaystackViewSet
from .serializers import SKUIndexSerializer


class SKUListView(ListAPIView):
    # queryset = SKU.objects.filter(is_launched=True,category_id=)
    # 重写方法，而不重写属性的原因：需要从请求路径中获取参数，用于查询
    # 获取参数需要得到视图对象，通过属性kwargs、args访问参数
    def get_queryset(self):
        return SKU.objects.filter(is_launched=True, category_id=self.kwargs.get('category_id'))

    serializer_class = SKUListSerializer

    # 分页
    pagination_class = StandardResultsSetPagination

    # 排序
    filter_backends = [OrderingFilter]
    ordering_fields = ['sales', 'create_time', 'price']


class SKUSearchViewSet(HaystackViewSet):
    """
    SKU搜索
    """
    index_models = [SKU]

    serializer_class = SKUIndexSerializer

    # 分页
    pagination_class = StandardResultsSetPagination
